package com.ccx.coachsite.ui.login;

import java.io.ByteArrayOutputStream;
import java.io.File;

import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.ccx.coachsite.bus.LoginBus;
import com.ccx.coachsite.bus.SharedKey;
import com.ccx.coachsite.bus.TianyiLoginBus;
import com.ccx.coachsite.util.SharedPreferencesUtil;
import com.ccx.coachsite.util.StringUtil;
import com.ccx.coachsite.util.CommonViewUtils;
import com.ccx.coachsite.CoachSiteMainActivity;
import com.ccx.coachsite.MainSplash;
import com.ccx.coachsite.R;
import com.tencent.weibo.webview.OAuthV1AuthorizeWebView;

public class LoginActivity extends CommonActivity {

	private View mLoginBtn; // 登录
	private View mCustomLoginBtn; // 登录
	private View mRegisterBtnSina; // sina注册
	private View mRegisterBtnQQ; // qq注册
	private View mRegisterBtnTianyi; // 手机注册

	private EditText mMobile; // 手机号码
	private EditText mPassword; // 密码

	LoginBus mLoginBus;

	public static final int NONE = 0;
	public static final int PHOTOHRAPH = 1;// 拍照
	public static final int PHOTOZOOM = 2; // 缩放
	public static final int PHOTORESOULT = 3;// 结果

	public static final String IMAGE_UNSPECIFIED = "image/*";
	ImageView imageView = null;
	Button btnRegister;
	Button button0 = null;
	Button button1 = null;
	
	private QQWeibo mQQWeibo;
	private SinaWeibo mSinaWeibo;

	@Override
	protected void initComponents() {

		mMobile = (EditText) this.findViewById(R.id.login_mobile);
		mPassword = (EditText) this.findViewById(R.id.login_password);
		btnRegister = (Button) findViewById(R.id.login_top_right);
		btnRegister.setBackgroundResource(R.drawable.login_right_empty_bg);
		btnRegister.setText("注册");
		btnRegister.setVisibility(View.VISIBLE);
		btnRegister.setOnClickListener(new OnRegisterListener());

		String mobile = SharedPreferencesUtil.getValue(LoginActivity.this,
				SharedKey.Shared_Mobile_Key, "");
		String password = SharedPreferencesUtil.getValue(LoginActivity.this,
				SharedKey.Shared_Password_Key, "");
		if (!StringUtil.isEmpty(mobile)) {
			mMobile.setText(mobile);
			mPassword.setText(password);
		}
		//mMobile.setText("18059139409");
		//mMobile.setText("15396205614");
		//mPassword.setText("123456");

		mLoginBtn = findViewById(R.id.login_btn_login);
		mLoginBtn.setOnClickListener(new OnLoginListener());

		mCustomLoginBtn = findViewById(R.id.login_btn_login_custom);
		mCustomLoginBtn.setOnClickListener(new OnCustomLoginListener());
		//mCustomLoginBtn.setVisibility(View.GONE);

		mRegisterBtnSina = findViewById(R.id.login_btn_sina);
		mRegisterBtnSina.setOnClickListener(new OnRegisterSinaListener());

		mRegisterBtnQQ = findViewById(R.id.login_btn_qq);
		mRegisterBtnQQ.setOnClickListener(new OnRegisterQQListener());

		mRegisterBtnTianyi = findViewById(R.id.login_btn_tianyi);
		mRegisterBtnTianyi.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				if (!CommonViewUtils.isNetConnectionAvailable(mContext)) {
					Toast.makeText(mContext, R.string.login_network_error, Toast.LENGTH_SHORT).show();
					return;
				}
				TianyiLoginBus loginBus = new TianyiLoginBus(LoginActivity.this);
				loginBus.doTianyiLogin(true);
				//Toast.makeText(mContext, "正在建设中，敬请期待", Toast.LENGTH_SHORT).show();
			}
		});
//		mReturnEnable = false;
		mQQWeibo = new QQWeibo(this);
		mSinaWeibo = new SinaWeibo(this);
	}

	private void customLogin() {
		final String mobile = mMobile.getText().toString().trim();
		final String password = mPassword.getText().toString().trim();

		if (mLoginBus == null) {
			mLoginBus = new LoginBus(this);
			mLoginBus.setxpert(true);
		}

		mLoginBus.login(mobile, password);
	}

	class OnCustomLoginListener implements OnClickListener {
		@Override
		public void onClick(View v) {
			Intent intent = new Intent(mContext, ForgetPwdActivity.class);
			startActivity(intent);
			//customLogin();
		}
	}

	private void testPhoto() {
		imageView = (ImageView) findViewById(R.id.imageID);
		button0 = (Button) findViewById(R.id.btn_01);
		button1 = (Button) findViewById(R.id.btn_02);

		button0.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(Intent.ACTION_PICK, null);
				intent.setDataAndType(
						MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
						IMAGE_UNSPECIFIED);
				startActivityForResult(intent, PHOTOZOOM);
			}
		});

		button1.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
				intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(
						Environment.getExternalStorageDirectory(), "temp.jpg")));
				startActivityForResult(intent, PHOTOHRAPH);
			}
		});
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (resultCode == NONE)
			return;
		if (requestCode==1)	{
			if (resultCode==OAuthV1AuthorizeWebView.RESULT_CODE)	{
				//从返回的Intent中获取验证码
				mQQWeibo.doQAuthResult(data);
				return;
			}
		}
		// 拍照
		if (requestCode == PHOTOHRAPH) {
			// 设置文件保存路径这里放在跟目录下
			File picture = new File(Environment.getExternalStorageDirectory()
					+ "/temp.jpg");
			startPhotoZoom(Uri.fromFile(picture));
		}

		if (data == null)
			return;

		// 读取相册缩放图片
		if (requestCode == PHOTOZOOM) {
			startPhotoZoom(data.getData());
		}
		// 处理结果
		if (requestCode == PHOTORESOULT) {
			Bundle extras = data.getExtras();
			if (extras != null) {
				Bitmap photo = extras.getParcelable("data");
				ByteArrayOutputStream stream = new ByteArrayOutputStream();
				photo.compress(Bitmap.CompressFormat.JPEG, 75, stream);// (0 -
																		// 100)压缩文件
				imageView.setImageBitmap(photo);
			}

		}

		super.onActivityResult(requestCode, resultCode, data);
	}

	public void startPhotoZoom(Uri uri) {
		Intent intent = new Intent("com.android.camera.action.CROP");
		intent.setDataAndType(uri, IMAGE_UNSPECIFIED);
		intent.putExtra("crop", "true");
		// aspectX aspectY 是宽高的比例
		intent.putExtra("aspectX", 1);
		intent.putExtra("aspectY", 1);
		// outputX outputY 是裁剪图片宽高
		intent.putExtra("outputX", 64);
		intent.putExtra("outputY", 64);
		intent.putExtra("return-data", true);
		startActivityForResult(intent, PHOTORESOULT);
	}

	private void login() {
		final String mobile = mMobile.getText().toString().trim();
		final String password = mPassword.getText().toString().trim();

		if (mLoginBus == null) {
			mLoginBus = new LoginBus(this);
		}

		mLoginBus.login(mobile, password);
	}

	class OnRegisterSinaListener implements OnClickListener {
		@Override
		public void onClick(View v) {
			mSinaWeibo.doQAuth();
			return;

//			Intent intent = new Intent(mContext, ThirdRegisterActivity.class);
//			intent.putExtra("ThirdToken", "sina");
//			startActivity(intent);
		}
	}

	class OnRegisterQQListener implements OnClickListener {
		@Override
		public void onClick(View v) {
			mQQWeibo.doQAuth();
			return;
//			Intent intent = new Intent(mContext, ThirdRegisterActivity.class);
//			intent.putExtra("ThirdToken", "qq");
//			startActivity(intent);
		}
	}

	class OnRegisterListener implements OnClickListener {
		@Override
		public void onClick(View v) {
			Intent intent = new Intent(mContext, RegisterActivity.class);
			startActivity(intent);
		}
	}

	// 登录
	class OnLoginListener implements OnClickListener {
		@Override
		public void onClick(View v) {
			
			CommonViewUtils.hideKeyboard(LoginActivity.this);
			Animation shake = AnimationUtils.loadAnimation(mContext,
					R.anim.shake);
			String mobile = mMobile.getText().toString().trim();
			String password = mPassword.getText().toString().trim();

			if (StringUtil.isEmpty(mobile)) {
				CommonViewUtils.showErrorByEditText(mMobile,
						getString(R.string.login_mobile_empty), shake);
				return;
			}
			if (!StringUtil.isMobile(mobile)) {
				CommonViewUtils.showErrorByEditText(mMobile,
						getString(R.string.login_not_mobile_number), shake);
				return;
			}
			if (StringUtil.isEmpty(password)) {
				CommonViewUtils.showErrorByEditText(mPassword,
						getString(R.string.login_password_empty), shake);
				return;
			}
			if (!CommonViewUtils.isNetConnectionAvailable(mContext)) {
				Toast.makeText(mContext, R.string.login_network_error,
						Toast.LENGTH_SHORT).show();
				return;

			}

			login();
		}
	}

	@Override
	protected int getMainContentViewId() {
		return R.layout.login_activity;
	}

	@Override
	protected Class<?> getResouceClass() {
		return R.class;
	}

	@Override
	protected void setCustomTitleTop() {
		TopViewUtil.updateTitle(this, R.id.login_top_title,
				getString(R.string.login_title));

		TopViewUtil.setTopBackVisable(this, R.id.login_btn_return, View.GONE);
	}

	@Override
	protected void initData() {
	}
}
